<?php
/**
 *
 */
class Sx_article extends CI_Controller {
	/*手机KEY*/
	var $postMobiKey;
	/*sessionID*/
	var $postSessionId;
	/*菜品名称*/
	var $pic_name;
	/*主图路径*/
	var $pic_path;
	/*菜品介绍*/
	var $pic_caption;
	/*餐馆名*/
	var $restaurant;
	/*所在城市*/
	var $city;
	/*X坐标*/
	var $pic_x;
	/*Y坐标*/
	var $pic_y;
	/*发表时间*/
	var $pic_time;
	/*餐馆地址*/
	var $pic_addr;
	/*执行动作*/
	var $postAction;
	/*用户数据*/
	var $userArr;
	/*用户ID*/
	var $userid;
	/*条件*/
	var $criteria;
	/*条件对应数组*/
	var $criArr = array("city", "user", "time", "latest", "tasty", "beautiful", "want");
	/* 数据库操作表*/
	var $tab;
	/*每次返回客户端的条数*/
	var $rNum = "10";
	var $cursor = 0;
	var $offset = 10;
	var $nicename;
	/*范围距离*/
	var $distance = 1000;

	public function index() {
		/*在如帖子操作mod*/
		$this -> load -> model("sx_article_mod");
		/*如果直接访问此页面acction*/
		$post = $this -> input -> post('action');
		if (!$post || empty($post)) {
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_no'), TRUE));
			exit ;
		} else {
			$this -> postAction = $this -> input -> post('action');
		}
		/*获取首页帖子显示*/
		if ($this -> postAction == "getArticleindex") {
			$this -> getArticleIndex();
			exit ;
		}
		/*获取最新帖子内容*/
		if ($this -> postAction == "newest") {
			$this -> newest();
			exit ;
		}
		/*获取评论*/
		if ($this -> postAction == "getComment") {
			$this -> get_comment();
			exit ;
		}
		/*判断用户权限*/
		$this -> postMobiKey = $this -> input -> post('mobikey');
		$this -> postSessionId = $this -> input -> post('sessionid');
		$this -> userArr = $this -> sx_useropen_mod -> user_con($this -> postMobiKey, $this -> postSessionId, TRUE);

		if (!$this -> userArr) {
			/*session或者Key过期，验证失败*/
			print($this -> fengchaoclass -> showhckmsg("-1", NULL, $this -> lang -> line('hck_no'), TRUE));
			exit ;
		}

		$this -> userid = $this -> userArr['userid'];
		$this -> nicename = $this -> userArr['nicename'];

		switch ($this -> postAction) {
			/* 执行add操作*/
			case 'addArticle' :
				$this -> addarticle();
				break;
			case 'addComment' :
				$this -> addcomment();
				break;
			/* 添加收藏*/
			case 'setFavorites' :
				$this -> setfavorites();
				break;
			/* 显示个人收藏*/
			case 'getFavorites' :
				$this -> getfavorites();
				break;
			/*删除个人收藏*/
			case 'delFavorites' :
				$this -> delfavorites();
				break;
			case 'setAppraise' :
				$this -> setappraise();
				break;
			case 'getAppraise' :
				$this -> getAppraise();
				break;
			default :
				print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_no'), TRUE));
				break;
		}
	}

	//------------------------------------------------------------------------------------------------------------------
	/**
	 * 添加帖子
	 */
	//------------------------------------------------------------------------------------------------------------------
	private function addarticle() {

		/*获取菜品名称*/
		$this -> pic_name = $this -> input -> post("pic_name");
		/*获取菜品图片的路径*/
		$this -> pic_path = $this -> input -> post("pic_path");
		/*获取城市*/
		$this -> city = $this -> input -> post('city');
		/*获取发贴人*/
		$this -> userid = $this -> userArr['userid'];
		/*获取餐馆名称*/
		$this -> restaurant = $this -> input -> post("restaurant");
		/*餐馆地址*/
		$this -> pic_addr = $this -> input -> post("pic_addr");
		/*菜品介绍*/
		$this -> pic_caption = $this -> input -> post("pic_caption");
		/*菜品坐标*/
		$this -> pic_x = $this -> input -> post("pic_x");
		$this -> pic_y = $this -> input -> post("pic_y");
		/* 定义数据库操作表*/
		$this -> tab = "sx_article";
		/*判断是否有空值*/
		if (empty($this -> userid) || empty($this -> pic_name) || empty($this -> pic_addr) || empty($this -> pic_path) || empty($this -> city) || empty($this -> restaurant)) {
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_article_add_null'), TRUE));
			exit ;
		}
		$dbArr = array("user_id" => $this -> userid, "pic_path" => $this -> pic_path, "pic_caption" => $this -> pic_caption, "user_nicename" => $this -> nicename, "pic_addr" => $this -> pic_addr, "pic_x" => $this -> pic_x, "pic_y" => $this -> pic_y, "pic_name" => $this -> pic_name, "restaurant" => $this -> restaurant, "city" => $this -> city);
		/*判断操作是否成功*/
		if ($this -> sx_article_mod -> addarticle($dbArr, $this -> tab) == FALSE) {

			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_article_add_N'), TRUE));
		} else {
			print($this -> fengchaoclass -> showhckmsg("0", NULL, $this -> lang -> line('hck_article_add_Y'), TRUE));
		}

	}

	//------------------------------------------------------------------------------------------------------------------
	/**
	 * 获取首页数据
	 * 最近最好
	 */
	//------------------------------------------------------------------------------------------------------------------
	private function getArticleIndex() {
		$this -> city = $this -> input -> post("city");
		$this -> cursor = $this -> input -> post("cursor");
		$this -> offset = $this -> input -> post("offset");
		$this -> x = $this -> input -> post('pic_x');
		$this -> y = $this -> input -> post('pic_y');
		$mydistance = $this -> input -> post("pic_distance");

		!empty($mydistance) ? $this -> distance = $mydistance : $this -> distance;
		
		
		if (!empty($this -> x) || !empty($this -> y)) {
			$n = $this -> sx_article_mod -> getindextasty($this -> x, $this -> y, $this -> distance, $this -> cursor, $this -> offset, $this -> city, TRUE);
			while ($n <= 6) {
				$this -> distance = $this -> distance * 10;
				$n = $this -> sx_article_mod -> getindextasty($this -> x, $this -> y, $this -> distance, $this -> cursor, $this -> offset, $this -> city, TRUE);
				if ($this -> distance > 1000000) {
					break;
				}
			}
		}else{
			$n = $this -> sx_article_mod -> articletotal($this -> city);
		}

		/*返回数据*/
		$db = $this -> sx_article_mod -> getindextasty($this -> x, $this -> y, $this -> distance, $this -> cursor, $this -> offset, $this -> city);
		$rArr = array('Msg_error' => "0", 'Msg' => "", 'MsgTime' => date("Y-m-d H:i:s"));
		/*返回条数*/
		$rArr['total'] = $n;
		if (empty($db)) {
			$rArr['Resource'] = 0;
			$rArr['Msg_error'] = "18";
		} else {
			$rArr['Resource'] = $db;
		}

		print(json_encode($rArr));
        //print_r($rArr);
	}

	//------------------------------------------------------------------------------------------------------------------
	/**
	 * 获取评价内容之最
	 */
	//------------------------------------------------------------------------------------------------------------------

	private function getAppraise() {
		$p = $this -> input -> post('appraise');
		if (!in_array($p, array("tasty", "beautiful", "want", "comment"))) {
			print($this -> fengchaoclass -> showhckmsg("", NULL, $this -> lang -> line('hck_parame'), TRUE));
			exit ;
		}

	}

	/**
	 * 获取最新数据
	 */
	private function newest() {
		$this -> city = $this -> input -> post("city");
		$this -> cursor = $this -> input -> post("cursor");
		$this -> offset = $this -> input -> post("offset");
		empty($this -> cursor) ? $this -> cursor = 0 : $this -> cursor;
		empty($this -> offset) ? $this -> offset = 10 : $this -> offset;
		$this -> city == "0" ? $this -> city = "" : $this -> city;
		$n = $this -> sx_article_mod -> articletotal($this -> city);

		if ($this -> cursor >= $n) {
			$db = 0;
		} else {
			$db = $this -> sx_article_mod -> getarticlelist($this -> city, $this -> cursor, $this -> offset);
		}

		$rArr = array('Msg_error' => "0", 'Msg' => "返回最新数据", 'MsgTime' => date("Y-m-d H:i:s"));
		$rArr['total'] = $n;
		if (empty($db)) {
			$rArr['Resource'] = 0;
			$rArr['Msg_error'] = "18";
		} else {
			$rArr['Resource'] = $db;
		}

		print(json_encode($rArr));
	}

	//------------------------------------------------------------------------------------------------------------------
	/**
	 * 获取评论
	 */
	//------------------------------------------------------------------------------------------------------------------
	private function get_comment() {
		$postpic_id = $this -> input -> post("pic_id");
		$this -> cursor = $this -> input -> post("cursor");
		$this -> offset = $this -> input -> post("offset");
		$db = $this -> sx_article_mod -> getcomment($postpic_id, $this -> cursor, $this -> offset);
		$rArr = array('Msg_error' => "0", 'Msg' => "", 'MsgTime' => date("Y-m-d H:i:s"));
		$rArr['total'] = $this -> sx_article_mod -> commnettotal($postpic_id);
		if (empty($db)) {
			$rArr['Resource'] = 0;
			$rArr['Msg_error'] = "18";
		} else {
			$rArr['Resource'] = $db;
		}
		$rArr['Resource'] = $db;
		print(json_encode($rArr));
	}

	//------------------------------------------------------------------------------------------------------------------
	/**
	 * 添加评论
	 */
	//------------------------------------------------------------------------------------------------------------------
	private function addcomment() {
		/*设定操作的表名*/
		$this -> tab = "sx_comment";
		/*获取要评论的帖子ID*/
		$pic_id = $this -> input -> post("pic_id");
		/*获取评论内容*/
		$content = $this -> input -> post("content");
		/*要添加的评论内容*/
		$dbArr = array("author" => $this -> userid, "pic_id" => $pic_id, "content" => $content);
		$db = $this -> sx_article_mod -> addarticle($dbArr, $this -> tab);
		if ($db == FALSE) {

			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_article_add_N'), TRUE));
		} else {
			$arrayName = array("new_id" => $db);
			print($this -> fengchaoclass -> showhckmsg("0", $arrayName, $this -> lang -> line('hck_article_add_Y'), TRUE));
		}

	}

	//------------------------------------------------------------------------------------------------------------------
	/**
	 * 添加个人收藏
	 */
	//------------------------------------------------------------------------------------------------------------------

	private function setfavorites() {
		/*操作收藏表*/
		$this -> tab = "sx_favorites";
		/*获取菜品id*/
		$picid = $this -> input -> post("pic_id");
		/*判断数据是否为空*/
		if (empty($picid) || empty($this -> userid)) {
			print($this -> fengchaoclass -> showhckmsg("", NULL, $this -> lang -> line('hck_parame'), TRUE));
			exit ;
		}
		/*插入数据库数据*/
		$dbArr = array("user_id" => $this -> userid, "pic_id" => $picid);

		if ($this -> sx_article_mod -> addarticle($dbArr, $this -> tab) == FALSE) {
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_article_add_N'), TRUE));
		} else {
			print($this -> fengchaoclass -> showhckmsg("0", NULL, $this -> lang -> line('hck_article_add_Y'), TRUE));
		}

	}

	//------------------------------------------------------------------------------------------------------------------
	/**
	 * 获取个人收藏
	 */
	//------------------------------------------------------------------------------------------------------------------
	private function getfavorites() {
		$this -> criteria = "";

	}

	//------------------------------------------------------------------------------------------------------------------
	/**
	 * 删除个人收藏
	 */
	//------------------------------------------------------------------------------------------------------------------
	private function delfavorites() {

	}

	/**
	 * 添加评价
	 */
	private function setappraise() {
		$pic_id = $this -> input -> post("pic_id");
		$p = $this -> input -> post('appraise');
		if (!in_array($p, array("tasty", "beautiful", "want"))) {
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_article_add_N'), TRUE));
			exit ;
		}
		if ($this -> sx_article_mod -> setappraise($pic_id, $p)) {
			print($this -> fengchaoclass -> showhckmsg("0", NULL, $this -> lang -> line('hck_article_add_Y'), TRUE));
		} else {
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_article_add_N'), TRUE));
		}
	}

}
?>